package com.lw.lost.activity

import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import com.lw.lost.activity.admin.PostManageActivity
import com.lw.lost.activity.admin.UserManageActivity
import com.lw.lost.activity.admin.ReportManageActivity
import com.lw.lost.activity.admin.FeedbackManageActivity

import com.lw.lost.databinding.ActivityAdminBinding
import com.lw.lost.db.DBlost
import com.lw.lost.utils.PreferenceUtils

class AdminActivity : AppCompatActivity() {

    private lateinit var binding: ActivityAdminBinding
    private lateinit var db: DBlost

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityAdminBinding.inflate(layoutInflater)
        setContentView(binding.root)

        setSupportActionBar(binding.toolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        supportActionBar?.title = "管理后台"

        db = DBlost(this)
        initViews()
        loadStatistics()
    }

    private fun initViews() {
        // 设置欢迎语
        binding.tvWelcome.text = "欢迎回来，${PreferenceUtils.getUserName(this)}"

        // 用户管理卡片点击事件
        binding.cardUserManage.setOnClickListener {
            startActivity(Intent(this, UserManageActivity::class.java))
        }

        // 帖子管理卡片点击事件
        binding.cardPostManage.setOnClickListener {
            startActivity(Intent(this, PostManageActivity::class.java))
        }

        // 举报管理卡片点击事件
        binding.cardReportManage.setOnClickListener {
            startActivity(Intent(this, ReportManageActivity::class.java))
        }

        // 反馈管理卡片点击事件
        binding.cardFeedbackManage.setOnClickListener {
            startActivity(Intent(this, FeedbackManageActivity::class.java))
        }

        // 退出登录按钮点击事件
        binding.btnLogout.setOnClickListener {
            PreferenceUtils.clearUserInfo(this)
            startActivity(Intent(this, LoginActivity::class.java))
            finish()
        }
    }

    private fun loadStatistics() {
        // 加载用户总数
        val userCursor = db.userDao.getAllUsers()
        binding.tvUserCount.text = "用户总数：${userCursor.count}"
        userCursor.close()

        // 加载帖子总数
        val postCursor = db.postDao.getAllActivePosts()
        binding.tvPostCount.text = "帖子总数：${postCursor.count}"
        postCursor.close()

        // 加载待处理举报数
        val reportCursor = db.reportDao.getReports()
        binding.tvReportCount.text = "待处理举报：${reportCursor.count}"
        reportCursor.close()
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == android.R.id.home) {
            finish()
            return true
        }
        return super.onOptionsItemSelected(item)
    }
} 